package org.poptest.populate.provider;

/**
 * Licensed under the Apache License, Version 2.0 (the "License");
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * @author Alan Roche
 *         Date: 14/02/11
 *         Time: 23:01
 */
public abstract class AbstractRangeProvider<T> implements ValueProvider{
    protected T value;
    private final T max;
    private final T min;
    private final Class<?> type;
    private boolean cycleNext = false;

    protected AbstractRangeProvider(Class<?> type, T min, T max) {
        this.value = min;
        this.max = max;
        this.min = min;
        this.type = type;
    }


    public T value() {
        final T returnValue = value;
        value = nextValue();
        return returnValue;
    }

    public T getMax() {
        return max;
    }

    public T getMin() {
        return min;
    }

    protected final T nextValue() {
        final T value;
        if (cycleNext) {
            value = getMin();
        }  else {
            value = doNextValue();
            if (value.equals(getMax())) {
                cycleNext = true;
            }
        }
        return value;
    }

    protected  abstract T doNextValue();

    public void setValue(T value) {
        this.value = value;
    }

    public Class<?> getType() {
        return type;
    }
}
